package com.ssm.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.ssm.entity.*;
import com.ssm.result.Result;
import com.ssm.service.*;
import com.ssm.vo.HouseQueryVo;
import com.ssm.vo.HouseVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author jie_ge
 * @Date 2022/4/20
 */
@RestController
@RequestMapping("/house")
@CrossOrigin
public class HouseController {
    @Reference
    private HouseService houseService;
    @Reference
    private CommunityService communityService;
    @Reference
    private DictService dictService;
    @Reference
    private HouseImageService houseImageService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private UserFollowService userFollowService;

    /**
     * 房源列表
     * @param houseQueryVo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/list/{pageNum}/{pageSize}")
    public Result findListPage(@RequestBody HouseQueryVo houseQueryVo,
                               @PathVariable Integer pageNum,
                               @PathVariable Integer pageSize) {
        PageInfo<HouseVo> pageInfo = houseService.findListPage(pageNum, pageSize, houseQueryVo);
        return Result.ok(pageInfo);
    }

    /**
     * 查看房源详情
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable Long id, HttpServletRequest request){
        House house = houseService.getById(id);
        Community community = communityService.getById(house.getCommunityId());
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        List<HouseImage> houseImageList = houseImageService.findList(id, 1);
        Map<String,Object> map = new HashMap<>();
        map.put("house",house);
        map.put("community",community);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseImage1List",houseImageList);
        //补充关注
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        Boolean isFollow = false;
        if(null != userInfo){
            Long userId = userInfo.getId();
            isFollow = userFollowService.isFollow(userId,id);
        }
        map.put("isFollow",isFollow);
        return Result.ok(map);
    }
}
